package com.ljpz.web.controller;
import com.ljpz.sso.query.bean.User;
import com.ljpz.web.bean.Userinfo;
import com.ljpz.web.interceptors.UserLoginHanderInterceptor;
import com.ljpz.web.service.UserinfoService;
import com.ljpz.web.threadLocal.UserThreadLocal;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by liujunchen on 2017/2/5.
 */
@RequestMapping("user/userinfo")
@Controller
public class UserInfoController {
    @Autowired
    private UserinfoService userinfoService ;
    @RequestMapping
      public ModelAndView  toMyinfomation(){
        ModelAndView  mv  = new ModelAndView("my-info")   ;
        //得到对应的
        User user = UserThreadLocal.get();
        //查询是否已经个人信息
         Userinfo userinfo  = this.userinfoService.queryUserinfo(user.getId());
         mv.addObject("userinfo"  , userinfo)  ;
         mv.addObject("useremail" , user.getEmail());
         return   mv ;
      }
    @RequestMapping("showing")
    public  ModelAndView toMyinforimage(){
          ModelAndView  mv = new ModelAndView("my-info-img");
          mv.addObject("userimage"  , this.userinfoService.queryUserinfo(UserThreadLocal.get().getId()).getUploadImage());
          return mv ;
      }
    @RequestMapping("more")
    public  ModelAndView toMyinfoMore(){
          ModelAndView mv = new ModelAndView("my-info-more");
          Userinfo userinfo = this.userinfoService.queryUserinfo(UserThreadLocal.get().getId());
          mv.addObject("userinfo" , userinfo)  ;
          return  mv  ;
      }

    /**
     * 修改个人资料
     *
     * 如果还没有创建就新增如果有记录则改为更新
     *
     * @param userinfo

     * @return
     */
    @RequestMapping(value = "modifyUser" ,  method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> modifyUserinformation(Userinfo  userinfo){
        Map<String ,Object> result  = new HashMap<String,Object>()  ;
        User user = UserThreadLocal.get();
        userinfo.setUserId(user.getId());
        if (this.userinfoService.mergeUserinfo(userinfo))
        {
            //用户信息提交修改成功
            result.put("status" , 200 ) ;
            result.put("data" , userinfo.getUserId()) ;
        }else {
            //用户信息提交修改失败
            result.put("status" , 500)  ;
        }
        return  result ;
    }

    @RequestMapping(value = "uploadImage"  , method = RequestMethod.POST)
    @ResponseBody
    public  Map<String, Object> uploaduserImage(@RequestParam("imageurl")String imageurl ){
        Map<String, Object> result = new   HashMap<String,Object>();
        User  user=   UserThreadLocal.get()  ;
        Userinfo userinfo = this.userinfoService.queryUserinfo(user.getId());
        if (userinfo ==null){
            userinfo.setUserId(user.getId());
            userinfo.setEmail(user.getEmail());
        }
        //修改对应的头像地址
        userinfo.setUploadImage(imageurl);
        if (this.userinfoService.mergeUserinfo(userinfo)){
            //用户提交修改头像成功
             result.put("status"  , 200 )   ;
             result.put("data", userinfo.getUserId())  ;
        }else {
            //失败
            result.put("status" , 500) ;
        }
        return result    ;
    }
}
